import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import { resolve } from "path"
import { fileURLToPath, URL } from "node:url"
import dts from "vite-plugin-dts"

export default defineConfig({
  plugins: [
    vue(),
    dts({
      entryRoot: "src",
      outDir: "dist/types",
      include: ["src/**/*.ts", "src/**/*.vue"],
      exclude: ["src/**/*.test.*", "src/**/*.spec.*", "src/main.ts", "src/App.vue"],
      staticImport: true,
      insertTypesEntry: true,
      copyDtsFiles: true
    })
  ],

  build: {
    lib: {
      entry: resolve(fileURLToPath(new URL(".", import.meta.url)), "src/index.ts"),
      name: "OfficialBlock",
      formats: ["es", "umd", "cjs"],
      fileName: (format) => `official-block.${format}.js`
    },
    minify: "esbuild",
    cssCodeSplit: false,
    cssMinify: 'esbuild',
    rollupOptions: {
      external: [
        "vue",
        "vuedraggable",
        "axios",
        "swiper",
        "swiper/vue",
        "swiper/modules",
        // 移除 swiper CSS 的外部化，让它们被打包进组件库
        // "swiper/css",
        // "swiper/css/navigation",
        // "swiper/css/pagination",
        // "swiper/css/autoplay",
        "@splidejs/vue-splide",
        // 移除 splide CSS 的外部化，让它们被打包进组件库
        // "@splidejs/vue-splide/css",
        "@arco-design/web-vue",
        "aos",
        "aos/dist/aos.css",
        "gsap",
        "simplebar-vue",
        "@wangeditor/editor",
        "@wangeditor/editor-for-vue",
      ],
      output: {
        globals: {
          vue: "Vue",
          vuedraggable: "vuedraggable",
          axios: "axios",
          swiper: "Swiper",
          "swiper/vue": "SwiperVue",
          "swiper/modules": "SwiperModules",
          "@splidejs/vue-splide": "VueSplide",
          "@arco-design/web-vue": "ArcoVue",
          aos: "AOS",
          gsap: "gsap",
          "simplebar-vue": "SimplebarVue",
          "@wangeditor/editor": "WangEditor",
          "@wangeditor/editor-for-vue": "WangEditorVue",
        },
        exports: "named",
        compact: true,
        generatedCode: {
          preset: "es2015"
        },
        assetFileNames: (assetInfo) => {
          if (assetInfo.type === 'asset' && assetInfo.fileName && assetInfo.fileName.endsWith('.css')) {
            return 'style.css'
          }
          return `[name][extname]`
        }
      }
    }
  },
  resolve: {
    alias: {
      "@": resolve(fileURLToPath(new URL(".", import.meta.url)), "src")
    }
  },
  css: {
    postcss: {
      plugins: []
    },
    preprocessorOptions: {
      scss: {
        api: 'modern-compiler',
        additionalData: (content, filename) => {
          // 只在 variables.scss 中导入，避免重复
          if (filename.includes("variables.scss") || filename.includes("global.scss")) {
            return content
          }
          // 只导入 variables，不导入 global（global 在 index.ts 中导入）
          return `@use "@/styles/variables.scss" as *;\n${content}`
        },
        includePaths: [resolve(fileURLToPath(new URL(".", import.meta.url)), "src")]
      }
    }
  },

  // 优化依赖预构建
  optimizeDeps: {
    exclude: ['vue']
  }
})
